<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>一起聊天吧~</title>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-top: 20px;
        }

        .chat-box {
            width: 540px;
            height: 400px;
            border: 1px solid #ccc;
            padding: 10px;
            overflow-y: auto;
            background: #f9f9f9;
        }

        .message {
            padding: 8px;
            margin: 5px;
            border-radius: 5px;
            max-width: 80%;
        }

        .user-message {
            background: #d1e7fd;
            align-self: flex-end;
        }

        .bot-message {
            background: #e1f3d8;
            align-self: flex-start;
        }

        .input-container {
            width: 540px;
            display: flex;
            margin-top: 10px;
        }

        .input-message {
            flex-grow: 1;
            height: 35px;
            padding: 5px;
            border: 1px solid #ccc;
        }

        .send-btn {
            width: 80px;
            height: 35px;
            margin-left: 5px;
            background-color: #007bff;
            color: white;
            border: none;
            cursor: pointer;
        }

        .send-btn:hover {
            background-color: #0056b3;
        }
    </style>
</head>

<body>
    <h2>一起聊天吧~</h2>
    <div class="chat-box" id="chatBox"></div>

    <div class="input-container">
        <input type="text" class="input-message" id="messageInput" placeholder="输入消息...">
        <button class="send-btn" onclick="Send()">发送</button>
    </div>

    <script>
        function Send() {
            let query = $(".input-message").val().trim();
            if (query === "") return; // 防止发送空消息
            
            BuildHtml(query, "user");

            // 模拟 AI 回复（你可以用 AJAX 替换这部分）
            setTimeout(() => {
                let response = "你好！我收到你的消息: " + query;
                BuildHtml(response, "bot");
            }, 1000);

            $(".input-message").val(""); // 清空输入框
        }

        function BuildHtml(message, sender) {
            let messageClass = sender === "user" ? "user-message" : "bot-message";
            let messageDiv = `<div class="message ${messageClass}">${message}</div>`;

            $("#chatBox").append(messageDiv);
            $("#chatBox").scrollTop($("#chatBox")[0].scrollHeight); // 自动滚动到底部
        }
    </script>
</body>

</html>
